package com.gw.quotes.activity;

import java.util.ArrayList;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gw.quotes.R;
import com.gw.quotes.entity.business.OrderInfoEntity;
import com.gw.quotes.entity.business.OrderInfoEntity.Order.OrderGood;
import com.gw.quotes.entity.business.OrderInfoEntity.Order.OrderGood.OrderAccessorie;
import com.gw.quotes.entity.business.OrderInfoEntity.Order.OrderGood.OrderOptional;
import com.yxw.view.NoScrollListView;

public class OrderDetailActivity extends BaseActivity {

	@ViewInject(id = R.id.back_img, click = "back")
	private ImageView back_img;
	@ViewInject(id = R.id.order_goods_img)
	private ImageView order_goods_img;
	@ViewInject(id = R.id.order_status)
	private TextView order_status;
	@ViewInject(id = R.id.text_final_total_price)
	private TextView text_final_total_price;
	@ViewInject(id = R.id.text_classname)
	private TextView text_classname;
	@ViewInject(id = R.id.order_goods_price)
	private TextView order_goods_price;
	@ViewInject(id = R.id.order_price)
	private TextView order_price;
	@ViewInject(id = R.id.user_name)
	private TextView user_name;
	@ViewInject(id = R.id.user_phone)
	private TextView user_phone;

	@ViewInject(id = R.id.order_no)
	private TextView order_no;
	@ViewInject(id = R.id.order_goods_name)
	private TextView order_goods_name;
	@ViewInject(id = R.id.order_num)
	private TextView order_num;
	@ViewInject(id = R.id.listview_optional)
	private NoScrollListView listview_optional;
	@ViewInject(id = R.id.layout_optional)
	private LinearLayout layout_optional;

	@ViewInject(id = R.id.listview_accessorie)
	private NoScrollListView listview_accessorie;
	@ViewInject(id = R.id.layout_accessorie)
	private LinearLayout layout_accessorie;

	private OrderInfoEntity orderInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_detail);
		getData();
		initview();
		// initview();
	}

	private void getData() {
		Intent intent = getIntent();
		// orderguid = intent.getStringExtra("Orderguid");
		orderInfo = (OrderInfoEntity) intent
				.getSerializableExtra("OrderInfoEntity");
	}

	private void initview() {
		if (orderInfo != null && orderInfo.getOrder() != null) {
			switch (orderInfo.getOrder().getStatus()) {
			case 2:
				order_status.setText("已确认");
				break;
			case 3:
				order_status.setText("已退回");
				break;

			default:
				order_status.setText("待确认");
				break;
			}
			order_price.setText(String.format("￥%.2f", orderInfo.getOrder()
					.getTotal()));
			user_name.setText(orderInfo.getOrder().getBuyerorgname());

			user_phone.setText(orderInfo.getOrder().getLinkmobile());

			text_classname.setText(orderInfo.getOrder().getGoodsclassname());
			order_no.setText(orderInfo.getOrder().getCode());
			order_num.setText(String.format("共%d件商品", orderInfo.getOrder()
					.getQty()));
			if (orderInfo.getOrder().getOrderGoodsList() != null
					&& orderInfo.getOrder().getOrderGoodsList().size() > 0) {
				OrderGood orderGood = orderInfo.getOrder().getOrderGoodsList()
						.get(0);
				order_goods_name.setText(orderGood.getGoodsname());
				order_goods_price.setText(String.format("￥%.2f",
						orderGood.getFinalprice()));
				text_final_total_price.setText(String.format("￥%.2f",
						(orderGood.getFinalprice() * orderInfo.getOrder()
								.getQty())));
				imageLoader.displayImage(orderGood.getImgurl(),
						order_goods_img, options);
			} else {
				order_goods_name.setText(orderInfo.getOrder()
						.getGoodsclassname());
			}
			if (orderInfo.getOrder().getOrdertype() == 1) {
				if (orderInfo.getOrder().getOrderGoodsList().get(0)
						.getOrderOptionalList().size() > 0) {
					layout_optional.setVisibility(View.VISIBLE);
					listview_optional.setAdapter(new OptionalListAdapter());
				}
				if (orderInfo.getOrder().getOrderGoodsList().get(0)
						.getOrderAccessorieList().size() > 0) {
					layout_accessorie.setVisibility(View.VISIBLE);
					listview_accessorie.setAdapter(new AccessorieListAdapter());
				}

			}
		}
	}

	public void back(View v) {
		finish();
	}

	/**
	 * 配件适配器
	 * 
	 * @author lenovo
	 *
	 */
	class OptionalListAdapter extends BaseAdapter {

		ArrayList<OrderOptional> list;

		public OptionalListAdapter() {
			try {
				list = orderInfo.getOrder().getOrderGoodsList().get(0)
						.getOrderOptionalList();
			} catch (Exception e) {
				list = new ArrayList<OrderOptional>();
			}
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(
						R.layout.item_order_optional, null);
			}
			TextView name = (TextView) convertView.findViewById(R.id.text_name);
			TextView price = (TextView) convertView
					.findViewById(R.id.text_total_price);
			TextView num = (TextView) convertView
					.findViewById(R.id.text_order_num);

			name.setText(list.get(position).getOptioncontent());
			price.setText(String.format("￥%.2f", (list.get(position)
					.getFinalprice() * list.get(position).getQty())));
			num.setText(String.format("共%d件商品", list.get(position).getQty()));
			return convertView;
		}

	}

	/**
	 * 附件适配器
	 * 
	 * @author lenovo
	 *
	 */
	class AccessorieListAdapter extends BaseAdapter {

		ArrayList<OrderAccessorie> list;

		public AccessorieListAdapter() {
			try {
				list = orderInfo.getOrder().getOrderGoodsList().get(0)
						.getOrderAccessorieList();
			} catch (Exception e) {
				list = new ArrayList<OrderAccessorie>();
			}
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(
						R.layout.item_order_accessorie, null);
			}
			TextView name = (TextView) convertView.findViewById(R.id.text_name);
			TextView total_price = (TextView) convertView
					.findViewById(R.id.text_total_price);
			TextView num = (TextView) convertView
					.findViewById(R.id.text_order_num);
			TextView price = (TextView) convertView
					.findViewById(R.id.text_price);
			ImageView img = (ImageView) convertView
					.findViewById(R.id.img_photo);

			name.setText(list.get(position).getGoodsname());
			price.setText(String.format("￥%.2f", list.get(position)
					.getFinalprice()));
			total_price.setText(String.format("￥%.2f", (list.get(position)
					.getFinalprice() * list.get(position).getQty())));
			num.setText(String.format("共%d件商品", list.get(position).getQty()));
			imageLoader.displayImage(list.get(position).getImgurl(), img,
					options);
			return convertView;
		}

	}

}
